<?php
/**
* @author Tibor Pino (tibor.pino@gmail.com)
* @license released under GNU Public License v. 2
* @copyright Copyright (c) 2007, Tibor Pino
*
*/

class CommentsController extends AppController
{
	var $name = "Comments";
	var $helpers = array('Rss','Text','Texy');
	var $uses = array('Comment');

	function beforeFilter() {
		parent::beforeFilter();
	}
	
	function admin_index() {
		$this->Comment->recursive = 1;
		$this -> paginate = array('order' => 'Comment.created desc');
		$this->set('comments', $this->paginate());
	}

	function admin_edit($id = null) {
		if (!$this -> Comment -> findById($id)) {
			$this->Session->setFlash(__('COMMENT_INVALID',true));
			$this->redirect(array('action'=>'index'), null, true);
		}
		if (!empty($this->data)) {
			if ($this->Comment->save($this->data)) {
				$this->Session->setFlash(__('COMMENT_SAVED_OK',true));
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('COMMENT_SAVED_ERROR');
			}
		} else {
			$this->data = $this->Comment->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (!$this -> Comment -> findById($id)) {
			$this->Session->setFlash(__('COMMENT_INVALID',true));
			$this->redirect(array('action'=>'index'), null, true);
		}
		if ($this->Comment->del($id)) {
			$this->Session->setFlash(__('COMMENT_DELETED_OK',true));
			$this->redirect(array('action'=>'index'), null, true);
		}
	}

	function admin_delete_comments() {
		if($this -> Comment -> deleteAll(array('Comment.id'=>$this -> data['CommentsDelete']))) {
			$this->Session->setFlash(__('COMMENTS_DELETED_OK',true));
		} else {
			$this->Session->setFlash(__('COMMENTS_DELETED_ERROR',true));
		}
		$this->redirect(array('action'=>'index'), null, true);
	}

	function admin_delete_spam() {
		if($this->Comment->deleteAll(array("antispam<>''"))) {
			$this->Session->setFlash(__('COMMENTS_DELETED_OK',true));
		} else {
			$this->Session->setFlash(__('COMMENTS_DELETED_ERROR',true));
		}
		$this->redirect(array('action'=>'index'), null, true);
	}
	
	function index() {
		$this->paginate = array('order'=>'Comment.created desc','conditions'=>'Comment.antispam ="" or Comment.antispam is null');
		$this->set('comments',$this->paginate());
	}
	
	function rss() {
		$this -> set('comments',$this -> Comment -> findAll(array('Comment.antispam is null or Comment.antispam=""'),null,array('Comment.created'=>'desc')));
	}
}
?>